package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.model.Prize;
import com.service.BranchService;
import com.service.PrizeService;

@Controller
public class PrizeController {
	@Autowired
	private PrizeService prizeService;
	@Autowired
	private BranchService branchService;
	

	// REquest to add prize.. Give blank form of prize
	@RequestMapping(value="/user/addPrize",method=RequestMethod.GET)
	public ModelAndView addPrize(@ModelAttribute("command") Prize prize) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("prizeList",prizeService.getAll());
		model.put("branches", branchService.getBranchName());
		return new ModelAndView("prize",model);
	}

	// add prize details into database
	@RequestMapping(value = "/savePrize", method = RequestMethod.POST)
	public String savePrize(@ModelAttribute("command") Prize prize) {
		Integer branchId=prize.getBranch().getBranchId();	
		prizeService.add(branchId,prize);
		return "redirect:/user/addPrize.htm";
	}

	// give prize details from database
	@RequestMapping("/user/editPrize")
	public ModelAndView editPrize(@ModelAttribute("command") Prize prize,
			HttpServletRequest request)
	{
		Integer id=Integer.parseInt(request.getParameter("id"));
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("prize",prizeService.get(id));
		return new ModelAndView("prize",model);
	}

	//delete Prize
	@RequestMapping("/user/deletePrize")
	public String deletePrize(HttpServletRequest request)
	{
		Integer id=Integer.parseInt(request.getParameter("id"));
		prizeService.delete(id);
		return "redirect:/user/addPrize.htm";
	}

}
